<?php
namespace app\sys\api;

use api\auth\Token;
use app\common\logic\AuthGroup;
use app\common\logic\Menu;
use app\common\logic\User;
use my\kaoqin\KaoQin;
use my\wechat\QyWeChat;
use think\Session;

class Open extends Common
{
    protected $_needCheckUser = false;
    protected $_needCheckToken = false;

    private $_wechat = null;

    public $error = '';

    private function wechat()
    {
        $c = [
            'appid' => c('wx_appid'),
            'appsecret' => c('wx_appsecret')
        ];

        if ($this->_wechat == null)
            $this->_wechat = new QyWeChat($c);

        return $this->_wechat;
    }

    public function login()
    {
        $user_id = input('user_id');

        if (empty($user_id)) {
            $auth_code = input('auth_code');
            if (empty($auth_code))
                return $this->error('用户名不能为空');

            $ret = $this->wechat()->getLoginInfo($auth_code);
            if (empty($ret) || !isset($ret['user_info']) || !isset($ret['user_info']['userid']) || empty($ret['user_info']['userid']))
                return $this->error('用户名不能为空');

            $user_id = $ret['user_info']['userid'];
        }

        Session::set('sid', $user_id);

        $config = $this->config();
        $user_info = User::where(['userid' => $user_id])->find();
        $menu = $this->_auth_menu_lists($user_id);

        $ret = [
            'config' => $config,
            'user_info' => $user_info,
            'menu' => $menu
        ];

        return $this->success($ret);
    }

    public function logout()
    {
        $access = input('server.http_access');
        if (empty($access))
            return $this->error("请稍后重试");

        session_id($access);
        Session::boot();
        Session::destroy();
        return $this->success(request()->domain() . '/sys/open/login');
    }

    private function config()
    {
        $config['access'] = session_id();

        $uid = function_exists('getUid') ? getUid() : '';
        $token = Token::update_token($uid, 3600 * 24 * 7);
        $config['token'] = $token;

        $res = config("app_res");
        if (!empty($res)) $config['res'] = $res;

        $is_release = config("app_is_release");
        if (!$is_release) $config['debug'] = true;

        return $config;
    }

    private function _auth_menu_lists($user_id)
    {
        $group_id = get_group_id($user_id);

        if (empty($group_id)) return [];

        $where['hide'] = 0;

        $AuthGroup = AuthGroup::get(['id' => $group_id]);
        $rules = $AuthGroup['rules'];

        if (empty($rules)) return [];
        else $where['id'] = ['in', $rules];

        $menu = Menu::where($where)->field('id,pid,title,desc,url,icon')->order('sort')->select();

        $ret = list2tree($menu);
        return $ret;
    }

    public function kq_oauth()
    {
        $c = [
            'appid' => '100018',
            'companyid' => '10840',
            'appsecret' => 'f33e6b9465c2bee511cb5e6a60794e43',
        ];

        $kaoQin = new KaoQin($c);
        $ret = $kaoQin->oauth();

        if ($ret === false)
            return $this->error($kaoQin->errCode);

        return $this->success($ret);
    }
}